<template>
  <div class="contentBox">
    <div class="search">
      <el-row :gutter="20" style="margin-right: 0">
        <el-col :span="6">
          <el-button
            type="primary"
            size="small "
            @click="addFooddialogVisible = true"
            >添加菜品</el-button
          >
        </el-col>
      </el-row>
    </div>
    <div class="tableBox">
      <el-table
        :data="tableData"
        border
        style="width: 100%; height: 100%; overflow: overlay"
        stripe
      >
        <el-table-column type="index" width="50" align="center">
        </el-table-column>
        <el-table-column
          prop="menuname"
          label="名称"
          align="center"
          width="140"
        >
        </el-table-column>
        <el-table-column
          prop="price"
          label="价格/元"
          align="center"
          width="160"
        >
        </el-table-column>

        <el-table-column prop="detatil" label="介绍" show-overflow-tooltip>
        </el-table-column>

        <el-table-column label="图片" align="center" width="240">
          <template slot-scope="scoped">
            <div class="imgBox">
              <img :src="scoped.row.photo" alt="" v-if="scoped.row.photo" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scoped">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scoped.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="mini"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scoped.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="text-align: center; padding-top: 16px">
      <el-pagination
        background
        small
        layout="prev, pager, next"
        :total="tableData.length"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="selectRow ? '编辑菜品' : '添加菜品'"
      :visible.sync="addFooddialogVisible"
      width="50%"
      @close="handleClose"
    >
      <el-form ref="form" :model="addFoodForm" label-width="80px">
        <el-form-item label="菜品名称">
          <el-input v-model="addFoodForm.menuname"></el-input>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-select v-model="addFoodForm.firstid" placeholder="请选择">
            <el-option
              v-for="(item, index) in firstMenuData"
              :key="index"
              :label="item.menuname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="addFoodForm.detatil"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="addFoodForm.price"></el-input>
        </el-form-item>
        <el-form-item label="菜品图片">
          <el-upload
            :auto-upload="false"
            :limit="1"
            name="img"
            class="foodCover"
            drag
            action=""
            :on-change="imgUpload"
            ref="img"
            :on-exceed="imgExceed"
            :on-remove="imgRemove"
            accept=".png"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <img
              v-else-if="selectRow"
              :src="addFoodForm.photo"
              class="avatar"
            />

            <i class="el-icon-plus" v-else>菜品图片</i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addFooddialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleupdateFooddialogConfirm"
          v-if="selectRow"
          >确 定</el-button
        >
        <el-button type="primary" @click="handleAddFooddialogConfirm" v-else
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 查询条件
      queryConditions: {},
      // 查询条件列表
      queryConditionsOptions: [
        {
          value: "name",
          label: "按姓名查询",
        },
        {
          value: "email",
          label: "按邮箱查询",
        },
      ],

      // 商品列表
      tableData: [
        {
          name: "香辣爆炒牛蛙",
          price: 59,
          introduction: "好吃的牛蛙",
          imgUrl: "",
        },
      ],
      addFooddialogVisible: false,
      addFoodForm: {
        price: 0,
        detatil: "",
        menuname: "",
        firstid: "",
        photo: "",
      },
      img: undefined,
      imgUrl: "",
      selectRow: undefined,
      firstMenuData: [],
    };
  },
  created() {
    this.getTableData();
    this.getFirstMenuData();
  },
  methods: {
    // 获取一级分类列表
    async getFirstMenuData() {
      const { data: res } = await this.$http.get(
        "/banservice/firstmenu/getfirstmenu"
      );
      this.firstMenuData = res.data.list;
      console.log(this.firstMenuData);
    },
    async handleAddFooddialogConfirm() {
      this.addFoodForm.price = this.addFoodForm.price - 0;
      const { data: res } = await this.$http.post(
        `/banservice/secondmenu/addsecondmenu`,
        this.addFoodForm
      );
      this.addFooddialogVisible = false;
      this.getTableData();
    },
    async handleupdateFooddialogConfirm() {
      this.addFoodForm.price = this.addFoodForm.price - 0;
      const { data: res } = await this.$http.post(
        `/banservice/secondmenu/updatesecondmenu`,
        this.addFoodForm
      );
      this.addFooddialogVisible = false;

      this.getTableData();
    },
    async getTableData() {
      const { data: res } = await this.$http.get(
        `/banservice/secondmenu/getallsecondmenu`
      );
      this.tableData = res.data.list;
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await this.$http.get(`/banservice/secondmenu/deletesecondmenu/${id}`);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getTableData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleClose() {
      this.selectRow = undefined;
      this.addFoodForm = {
        price: 0,
        detatil: "",
        menuname: "",
        firstid: "",
        photo: "",
      };
      this.imgRemove();
    },
    handleEdit(row) {
      this.selectRow = row;
      this.addFoodForm = row;
      this.addFooddialogVisible = true;
    },
    async imgUpload(file) {
      let types = ["png"];
      let name = file.name;
      let temp = name.lastIndexOf(".") + 1;
      const index = types.indexOf(name.slice(temp));
      const isLt2M = file.raw.size / 1024 / 1024 < 2;
      if (index === -1) {
        this.$message.error("png格式!");
        this.$refs.img.uploadFiles.length = 0;
        return false;
      }
      if (!isLt2M) {
        this.$message.error("图片大小不能超过 2MB!");
        this.$refs.img.uploadFiles.length = 0;
        return false;
      }
      this.imgUrl = URL.createObjectURL(file.raw);
      let fd = new FormData();
      fd.append("file", file.raw);

      const { data: res } = await this.$http.post(
        `/banservice/secondmenu/uploadOssFile`,
        fd
      );

      this.addFoodForm.photo = res.data.photo;
    },
    imgRemove() {
      this.imgUrl = "";
      this.img = "";
      this.$refs.img.clearFiles();
    },
    imgExceed() {
      this.$message.error("图片只能上传一个");
    },
  },
};
</script>
<style lang="less" scoped>
.contentBox {
  .search {
    padding-bottom: 20px;
  }
  .tableBox {
    height: 63vh;
  }
  .foodCover {
    position: relative;
    /deep/ .el-upload-dragger {
      font-size: 25px;
      color: #c0c4cc;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    img {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      object-fit: cover;
    }
  }
  .imgBox {
    position: relative;
    width: 200px;
    height: 120px;
    margin: 0 auto;
    cursor: pointer;
    img {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      object-fit: contain;
    }
  }
}
</style>
